/**
 * 
 */
package com.geekteam.manga.sync;

import android.app.IntentService;
import android.content.Intent;

/**
 * @author MtRol
 * 
 */
public class SyncService extends IntentService {

	public SyncService() {
		super("SyncService");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		
	}

	/*private void performGetCategory(Intent _intent){
		SyncProcess process = SyncProcess.getInstance(RealApplication
				.getInstance());
		if (process == null) {
			return;
		}
		boolean isFavorite = _intent.getBooleanExtra(Constant.CATEGORY_FAVORITE, false);
		ArrayList<CategoryModel> listCategory = process.getListCategory(isFavorite);
		Intent intent;
		if(isFavorite){
			intent = new Intent(Constant.FILTER_GET_NEW_CATEGORY);
		}else{
			intent = new Intent(Constant.FILTER_GET_CATEGORY);
		}
		intent.putParcelableArrayListExtra(Constant.LIST_CATEGORY, listCategory);
		RealApplication.getInstance().sendBroadcast(intent);
	}
	
	private void performGetSetting() {
		SyncProcess process = SyncProcess.getInstance(RealApplication
				.getInstance());
		if (process == null) {
			return;
		}

		ArrayList<CategoryModel> listCategory = process.getListCategory(true);
		ArrayList<CityModel> listCity = process.getListCity();
		ArrayList<SiteModel> listSite = process.getListSite();

		Intent intent = new Intent(Constant.FILTER_GET_SETTING);
		intent.putParcelableArrayListExtra(Constant.LIST_CATEGORY, listCategory);
		intent.putParcelableArrayListExtra(Constant.LIST_CITY, listCity);
		intent.putParcelableArrayListExtra(Constant.LIST_SITE, listSite);
		RealApplication.getInstance().sendBroadcast(intent);
	}

	private void performUpdateSetting(Intent _intent) {
		SyncProcess process = SyncProcess.getInstance(RealApplication
				.getInstance());
		if (process == null) {
			return;
		}
		// update sharedpreference and database
		ArrayList<ItemSetting> items = _intent
				.getParcelableArrayListExtra(Constant.ITEM_SETTING);
		String keyword = _intent.getStringExtra(Constant.KEYWORD);
		int type = _intent.getIntExtra(Constant.TYPE, 3);
		process.updateSettingDatabase(items, keyword, type);

		// send broadcast
		Intent intent = new Intent(Constant.FILTER_UPDATE_SETTING);
		RealApplication.getInstance().sendBroadcast(intent);
	}
	
	private void performUpdateCategory(Intent _intent) {
		SyncProcess process = SyncProcess.getInstance(RealApplication
				.getInstance());
		if (process == null) {
			return;
		}
		// update sharedpreference and database
		ArrayList<ItemSetting> items = _intent
				.getParcelableArrayListExtra(Constant.ITEM_SETTING);
		process.updateSettingDatabase(items, "", -1);

		// send broadcast
		Intent intent = new Intent(Constant.FILTER_UPDATE_CATEGORY);
		RealApplication.getInstance().sendBroadcast(intent);
	}

	private void performGetJob(Intent intent) {
		boolean isFavorite = intent.getBooleanExtra(Constant.JOB_ISFAVORITE, false);
		if(isFavorite){
			getJobFromDatabase(isFavorite);
		}else{
			getJobFromServer(intent);
		}
	}
	
	private void getJobFromServer(Intent intent){
		boolean isFirstLoad = intent.getBooleanExtra(Constant.JOB_FIRST_LOAD, false);
		//get job from database and then request server
		if(isFirstLoad){
			getJobFromDatabase(false);
		}
		
		//request server
		String lastId = intent.getStringExtra(Constant.JOB_LAST_ID);
		int pageSize = intent.getIntExtra(Constant.JOB_SIZE, 10);
		SyncProcess process = SyncProcess.getInstance(RealApplication
				.getInstance());
		if (process == null) {
			return;
		}
		ArrayList<ParcelableNameValuePair> param = process.addParamGetJob(lastId, pageSize);
		Jobs job = RestHelper.getJob(param);
		// TODO : Toannv Can xem xet viec delete job cu o day
		process.insertListJob(job, isFirstLoad);
		
		//send broadcast to UI
		Intent sendData = new Intent(Constant.FILTER_GET_JOB_SERVER);
		sendData.putExtra(Constant.JOB_FIRST_LOAD, isFirstLoad);
		sendData.putExtra(Constant.JOB_OBJECT, job);
		RealApplication.getInstance().sendBroadcast(sendData);
	}
	
	private void getJobFromDatabase(boolean isfavorite){
		SyncProcess process = SyncProcess.getInstance(RealApplication
				.getInstance());
		if (process == null) {
			return;
		}
		ArrayList<JobModel> listJobModel = process.getListJob(isfavorite);
		
		Intent intent = new Intent(Constant.FILTER_GET_JOB_DATABASE);
		intent.putParcelableArrayListExtra(Constant.LIST_JOB, listJobModel);
		intent.putExtra(Constant.FROM_DATABASE, isfavorite);
		
		RealApplication.getInstance().sendBroadcast(intent);
	}*/
}
